<?php
/**
 * ClassName: card_cate
 * Description: 会员卡分类控制器
 * @Author 周昌
 * @Create 2023/11/16 11:31
 */

namespace app;


use epii\server\Args;
use model\CardCateModel;
use service\admin\HospitalService;
use service\admin\CardCateService;

class card_cate extends base
{
    public function init()
    {
        parent::init();
        $this->accessDenied();
    }
    public function index()
    {
        if($this->adminInfo['role']==1){
            $this->assign('hospitals',HospitalService::getSelect());
        }
        //筛选
        $this->_as_type = CardCateService::cardCateTypeSelect();
        $this->_as_useLimit = CardCateService::cardCateUseLimitSelect();
        $this->adminUiDisplay();
    }

    public function ajax_data()
    {
        $map = [];
//        //登录用户权限判断
//        $hospital_id=$this->adminInfo['hospital_id'];
//        if($hospital_id){
//            $map[]=['hospital_id','=',$hospital_id];
//        }
        //根据医院组展示
        $hospital_group_id=$this->adminInfo['hospital_group_id'];
        if($hospital_group_id){
            $map[]=['hospital_group_id','=',$hospital_group_id];
        }

        $name = Args::params('name');
        if ($name) {
            $map[] = ['name', "LIKE", '%' . $name . '%'];
        }
        //卡类型
        $type = Args::params('type/d');
        if (is_int($type)) {
            $map[] = ['type', "=", $type];
        }
        //使用范围
        $use_limit = Args::params('use_limit/d');
        if (is_int($use_limit)) {
            $map[] = ['use_limit', "=", $use_limit];
        }

        
        $hospital_id = Args::params('hospital_id');
        if ($hospital_id) {
            $map[] = ['hospital_id', "=", $hospital_id];
        }
        echo CardCateService::getAjaxData($map);
    }

    public function add_edit()
    {
        $id = Args::params('id');
        if ($_SERVER['REQUEST_METHOD'] == "POST") {
            $data = Args::params('r');
            //登录权限判断
            $hospital_id=$this->adminInfo['hospital_id'];
            if(!empty($hospital_id)){
                $data['hospital_id']=$hospital_id;
                $data['hospital_group_id']=$this->adminInfo['hospital_group_id'];
            }
            //验证
            $this->validate($data,[
                'name|名称'=>'require|max:20',
                'hospital_id|所属医院'=>'require|integer|>=:0',
                'hospital_group_id|所属医院组'=>'require|integer|>=:0',
                'status|状态'=>'require|integer',
                'use_limit|使用范围'=>'require|integer',
                'type|类型'=>'require|integer',
                'notes_msg|描述'=>'max:200',
            ]);
            if ($id) {
                $res = CardCateService::edit($id,$data);
            } else {
                $data['addtime']=time();
                $res = CardCateService::add($data);
            }
            $res['code']?$this->success($res['msg']):$this->error($res['msg']);
        }
        $this->_as_info = $id ? CardCateModel::getInfo(['id'=>$id]) : [];
        $this->_as_id = $id;
        $this->_as_status = CardCateService::statusSelect();
        $this->_as_type = CardCateService::cardCateTypeSelect();
        $this->_as_useLimit = CardCateService::cardCateUseLimitSelect();
        $this->_as_hospitals = HospitalService::getSelect();

        $this->adminUiDisplay();
    }
    //修改状态
    public function status(){
        $id=Args::params('id/d/1','id,不能为空');
        $status=Args::params('status');
        $res=CardCateService::status($id,$status);
        if($res['code']){
            $this->success('成功','refresh');
        }
        $this->error();
    }

    /**
     * @return array|false|string* 删除
     */
    public function del()
    {
        $id = Args::params('id');
        //todo 判断分类下是否已经有，无则可以删除
        $res = CardCateService::del($id);
        if ($res['code']) {
            $this->success($res['msg'],'refresh');
        } else {
            $this->error($res['msg']);
        }
    }

}